#include "includes.h"
#include "common.h"
#include "defines.h"
#include "particles.h"

SDL_Surface* screen;

static int current_type = PT_NONE;
static int holding = 0;
static int x = 0 , y = 0;
static int update = 0;
static clock_t lastclock;


int main(int argc, char** argv)
{
    // initialize SDL video
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);
    // create a new window
#ifdef OpenGL
    screen = SDL_SetVideoMode(XRES, YRES, COLOR_BIT,
                              SDL_OPENGL);
#else   // OpenGL
    screen = SDL_SetVideoMode(XRES, YRES, COLOR_BIT,
                              SDL_HWSURFACE | SDL_DOUBLEBUF);
#endif  // OpenGL
#ifdef OpenGL
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
#endif  // OpenGL

    if (!screen) {
        printf("Unable to set %dx%d video: %s\n",
               XRES, YRES,
               SDL_GetError());
        return 1;
    }

    // program main loop
    int done = 0;

    init_parts();
    init_pmap();
    srand(NULL);

    lastclock = clock();

    while (!done) {
        // message processing loop
        SDL_Event event;

        while (SDL_PollEvent(&event)) {
            // check for messages
            switch (event.type) {
                // exit if the window is closed
            case SDL_QUIT:
                done = 1;
                break;
                // check for keypresses
            case SDL_MOUSEBUTTONDOWN:
                if (event.button.button == SDL_BUTTON_LEFT)
                    current_type = PT_WATR;
                else
                    current_type = PT_GAS;
                x = event.button.x;
                y = event.button.y;
                holding = 1;
                break;
            case SDL_MOUSEMOTION:
                x = event.motion.x;
                y = event.motion.y;
                break;
            case SDL_MOUSEBUTTONUP:
                holding = 0;
                break;
            case SDL_KEYDOWN: {
                    // exit if 'Q' is pressed
                    if (event.key.keysym.sym == 'q') {
                        done = 1;
                    } else if (event.key.keysym.sym == ' ') {
                        update = !update;
                    }

                    break;
                }
            } // end switch
        } // end of message processing
        // begin create particles
        if (holding) {
            create_part(x, y, current_type);
        }

        // end create particles

        // DRAWING STARTS HERE
        // clean screen
#ifdef OpenGL
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#else   // OpenGL
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
#endif  // OpenGL
        // begin drawing
        // todo:
        if (clock() - lastclock > MIN_UPDATE_INTERVAL) {
            if (update)
                global_update();
            lastclock = clock();
        }
        draw_all();
        // end drawing
        // DRAWING ENDS HERE
        // finally, update the screen :)
        // Flip
#ifdef OpenGL
        SDL_GL_SwapBuffers();
#else   // OpenGL
        SDL_Flip(screen);
#endif  // OpenGL
    } // end main loop

    return 0;
}
